/*
 * Copyright (C) 2012 by Hao Hou<ghost89413@gmail.com>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>*
 */
#include <gtk/gtk.h>
#include <imwindow.h>
#include <gdk/gdkx.h>
#define MAXCANDN 20
GtkWidget* imwindow;
GtkWidget* impclabel;
GtkWidget* impan;
GtkWidget* imcandtab;
GtkWidget* imcandlab[MAXCANDN];
GtkWidget* imframe;
uint32_t imxwin;
int InitIMWindow()
{
	//upper
	impclabel = gtk_label_new("");
	gtk_label_set_single_line_mode(GTK_LABEL(impclabel),1);
	gtk_label_set_justify(GTK_LABEL(impclabel),GTK_JUSTIFY_LEFT);
	gtk_misc_set_alignment(GTK_MISC(impclabel),0,0.5);
	//lower
	imcandtab = gtk_table_new(1,1,1);
	gtk_table_set_homogeneous(GTK_TABLE(imcandtab),0);
	gtk_table_set_col_spacings(GTK_TABLE(imcandtab),15);
	int i;
	for(i =  0; i < MAXCANDN; i ++)
		imcandlab[i] = gtk_label_new("");
	gtk_table_attach_defaults(GTK_TABLE(imcandtab),GTK_WIDGET(imcandlab[0]),0,1,0,1);

	impan = gtk_vpaned_new();
	gtk_paned_add1(GTK_PANED(impan),impclabel);
	gtk_paned_add2(GTK_PANED(impan),imcandtab);

	imframe = gtk_frame_new(NULL);
	gtk_frame_set_shadow_type(GTK_FRAME(imframe), GTK_SHADOW_IN);
	gtk_container_add(GTK_CONTAINER(imframe), GTK_WIDGET(impan));
		
	imwindow = gtk_window_new(GTK_WINDOW_POPUP);
	gtk_window_set_policy(GTK_WINDOW(imwindow),0,0,1);
	gtk_window_set_keep_above(GTK_WINDOW(imwindow), 1);
	gtk_container_add(GTK_CONTAINER(imwindow), GTK_WIDGET(imframe));
	
	return 0;	

}
int UpdateIMWindow(const char* preedit,char** candidates, int ncandidates)
{
gdk_threads_enter();
	if(ncandidates > MAXCANDN) ncandidates = MAXCANDN;
	guint row,col;
	int i;
	if(preedit)
	{
		gtk_label_set_markup(GTK_LABEL(impclabel), preedit);
	}
	if(candidates)
	{
		gtk_table_get_size(GTK_TABLE(imcandtab), &row, &col);
		if(col < ncandidates)
		{
			gtk_table_resize(GTK_TABLE(imcandtab),1, ncandidates);
			for(i = col; i < ncandidates; i ++)
			{
				gtk_table_attach_defaults(GTK_TABLE(imcandtab),
							  GTK_WIDGET(imcandlab[i]),
							  i,i+1,0,1);
				gtk_widget_show(GTK_WIDGET(imcandlab[i]));
			}
		}
		else if(col > ncandidates)
		{
			for(i = ncandidates; i < col; i ++)
			{
				/*gtk_container_remove(GTK_CONTAINER(imcandtab),
						     GTK_WIDGET(imcandlab[i]));
				gtk_object_destroy(GTK_OBJECT(imcandlab[i]));
				imcandlab[i] = gtk_label_new(NULL);*/
				gtk_label_set_markup(GTK_LABEL(imcandlab[i]),"");	
			}
		}
		for(i = 0; i < ncandidates; i ++)
			gtk_label_set_markup(GTK_LABEL(imcandlab[i]), candidates[i]);
	}
gdk_threads_leave();
	return 0;
}
int HideIMWindow()
{
	gdk_threads_enter();
	gtk_widget_hide_all(imwindow);
	gdk_threads_leave();
	return 0;
}
int ShowIMWindow()
{
	gdk_threads_enter();
	gtk_widget_show_all(imwindow);
	imxwin = GDK_WINDOW_XWINDOW(GTK_WIDGET(imwindow)->window);
	gdk_threads_leave();
	return 0;
}
